import 'rxjs/add/operator/toPromise';
import { Injectable } from '@angular/core';
import { Api } from './api.service';
import {MatSnackBar} from '@angular/material';
import {SnackSuccessComponent} from '../page/snack-success/snack-success.component';
import {SnackErrorComponent} from '../page/snack-error/snack-error.component';
import {SnackService} from './snack.service';
import {Router} from '@angular/router';
import {AuthService} from './auth.service';

@Injectable()
export  class LoginService {
  constructor(
    public api: Api,
    public snackBar: MatSnackBar,
    public snackService: SnackService,
    public router: Router,
    public authService: AuthService
  ) {}
  // 企业账号登陆
  companyLogin(companyInfo: any) {
    const seq = this.api.post('login/company', companyInfo);
    seq.subscribe((res: any) => {
      sessionStorage.setItem('isLoggedIn', 'true');
      sessionStorage.setItem('isCompany', 'true');
      sessionStorage.setItem('isTalent', 'false');
      sessionStorage.setItem('account', JSON.stringify(res));
      this.authService.loadData();
      this.snackService.message = '登陆成功';
      this.snackBar.openFromComponent(SnackSuccessComponent, {
        duration: 3000,
      });
      this.returnHome();
    }, err => {
      console.error('ERROR', err);
      this.snackService.message = err.error.message;
      this.snackBar.openFromComponent(SnackErrorComponent, {
        duration: 3000,
      });
    });

    return seq;
  }
  // 企业账号找回密码
  findPasswordInCompany(companyInfo: any) {
    const seq = this.api.post('findPassword/company', companyInfo);
    seq.subscribe((res: any) => {
      if (res == 'OK') {
        this.snackService.message = '密码找回成功,请在邮箱确认';
        this.snackBar.openFromComponent(SnackSuccessComponent, {
          duration: 3000,
        });
      } else {
      }
    }, err => {
      console.error('ERROR', err);
      this.snackService.message = err.error.message;
      this.snackBar.openFromComponent(SnackErrorComponent, {
        duration: 3000,
      });
    });

    return seq;
  }
  // 求职者登陆
  talentLogin(talentInfo: any) {
    const seq = this.api.post('login/talent', talentInfo);
    seq.subscribe((res: any) => {
      sessionStorage.setItem('isLoggedIn', 'true');
      sessionStorage.setItem('isCompany', 'false');
      sessionStorage.setItem('isTalent', 'true');
      sessionStorage.setItem('account', JSON.stringify(res));
      this.authService.loadData();
      this.snackService.message = '登陆成功';
      this.snackBar.openFromComponent(SnackSuccessComponent, {
        duration: 3000,
      });
      this.returnHome();
    }, err => {
      console.error('ERROR', err);
      this.snackService.message = err.error.message;
      this.snackBar.openFromComponent(SnackErrorComponent, {
        duration: 3000,
      });
    });

    return seq;
  }
  // 求职者账号找回密码
  findPasswordInTalent(talentInfo: any) {
    const seq = this.api.post('findPassword/talent', talentInfo);
    seq.subscribe((res: any) => {
      if (res == 'OK') {
        this.snackService.message = '密码找回成功,请在邮箱确认';
        this.snackBar.openFromComponent(SnackSuccessComponent, {
          duration: 3000,
        });
      } else {
      }
    }, err => {
      console.error('ERROR', err);
      this.snackService.message = err.error.message;
      this.snackBar.openFromComponent(SnackErrorComponent, {
        duration: 3000,
      });
    });

    return seq;
  }

  returnHome() {
    this.router.navigate(['/admin']);
  }

  logout(): void {
    sessionStorage.clear();
    this.authService.loadData();
  }
}
